<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" :cols='2'/>
    <FilterTable
      ref="table"
      :columns="columns"
      :dataSource="tableList"
      :onColumnsChange="columns => this.columns = columns"
      :isSelectColumn="false"
    >
    <template slot="controls" slot-scope="props">
        <multiuse-button  size="small" type="primary" @click="addHandle(1)" >选择产品组</multiuse-button>
      </template>
    </FilterTable>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <!-- <el-button @click="closeDrawer(2)" type="primary">保 存</el-button> -->
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>
    <BaseDialog
      :visible.sync="isseacher"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeachers   @close="closeHandlerOwner" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="isproduct"
      :title="'产品组查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <productSeacher   @close="closeHandlerProduct" @addchange="addChangeProduct"/>
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import {getList,getDetail,postSave} from '@/api/omSale/vehicleParameters/saleAuthorization/index.js'
import ownerSeachers from './ownerSeachers'
import productSeacher from './productSeacher'
export default {
  data(){
    this.BaseTable=null;
    this.saveData=null;
    return{
      formList:this.createdFrom(),
      columns:this.createdTable(),
      isseacher:false,
      isproduct:false,
      tableList:[]
    }
  },
  mixins:[dictionary],
  components: {
    ownerSeachers,
    productSeacher
  },
  props:['rowData'],
  created(){
    this.getDetail(this.rowData)
  },
  mounted(){
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change:p=>{
            if(!p){
              this.dealerCode=''
            }
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003'),
          initialValue:10031001,
          rules: [{ required: true, message: '请选择', trigger: 'change' }],

        },
        // {
        //   type: 'INPUT',
        //   label: '产品组',
        //   fieldName: 'cpz',
        //   placeholder: '请选择',
        //   unitRender: () => {
        //     return (
        //       <div>
        //         <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
        //       </div>
        //     );
        //   }
        // },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          placeholder: '请输入',
          selfCols:2

        },
      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button  size="mini" type="text" onClick={() => this.delHandle(props.row)}>
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '产品组代码',
          dataIndex: 'groupCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '产品组名称',
          dataIndex: 'groupName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ]
    },
    async getDetail(data){
      let res=await getDetail({dlrGroupId :data.id})
      for (let i in res.data) {
        this.formList.map(x => {
          if (i == x.fieldName) {
            x.initialValue = res.data[i];
          }
        });
      }
      this.dealerCode=res.data.dealerCode;
      this.tableList=res.data.groups;
    },
    changeHandle(val){
      this.saveData=val;
    },
    async closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        if(this.tableList.length<1){
          this.$notify.error({
            title: '错误',
            message: '请选择产品组',
          });
          return
        }
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          let arr=[]
          this.tableList.map(x=>{
            arr.push({groupCode:x.groupCode,groupId:x.groupId,groupName:x.groupName})
          })
          json.groups=arr;
          json.dealerCode=this.dealerCode;
          json.id=this.rowData.id
          console.log(json);
          let res = await postSave(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          }else{
            this.saveData=null;
          }
        }

      }
    },
    seacherFun(type){
      if(type==1){
        this.isseacher=true;
      }
    },
    addHandle(){
      this.isproduct=true;
    },
    delHandle(data){
      this.BaseTable.EXECUTE_DELETE([data])
    },
    addChangeProduct(val){
      this.isproduct=false;
      this.tableList=val||[];
    },
    closeHandlerOwner(val){
      if(val){
        this.formList.find(x=>x.fieldName=='dealerName').initialValue=val.dealerName;
        this.dealerCode=val.dealerCode;
      }
      this.isseacher=false;
    },
    closeHandlerProduct(){
      this.isproduct=false;
    }
  }
}
</script>
